package com.beyond.trace.controller.agricultural;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.beyond.trace.domain.agricultural.AgriculturalIdentify;
import com.beyond.trace.domain.agricultural.AgriculturalSuggest;
import com.beyond.trace.service.agricultural.IAgriculturalIdentifyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 农技病害查询
 * @Author: yaojianbo
 * @Date: 2023-05-22 11:37
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/agricultural")
@Api(value = "农技病害查询", tags = "农技病害查询")
public class AgriculturalIdentifyController extends BaseController {

    @Autowired
    private IAgriculturalIdentifyService agriculturalIdentifyService;

    @GetMapping("/getIdentifyList")
    @ApiOperation("查询农技病害查询列表")
    public TableDataInfo list(AgriculturalIdentify agriculturalIdentify)
    {
        startPage();
        List<AgriculturalIdentify> list = agriculturalIdentifyService.getIdentifyList(agriculturalIdentify);
        return getDataTable(list);
    }


    @GetMapping(value = "/getIdentifyById")
    @ApiOperation("通过id查询农技病害查询")
    public AjaxResult getInfo(Long id)
    {
        if(id == null) {
            return AjaxResult.error("id不能为空");
        }
        return AjaxResult.success(agriculturalIdentifyService.getById(id));
    }

    @PostMapping("/addIdentify")
    @ApiOperation("新增农技病害查询")
    public AjaxResult add(@RequestBody AgriculturalIdentify agriculturalIdentify)
    {
        agriculturalIdentify.setId("");
        agriculturalIdentify.setCreateTime(DateUtils.getNowDate());
        agriculturalIdentify.setUpdateTime(DateUtils.getNowDate());
        agriculturalIdentify.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        agriculturalIdentify.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        agriculturalIdentify.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        agriculturalIdentify.setStatus(0);
        return toAjax(agriculturalIdentifyService.save(agriculturalIdentify));
    }

    @PostMapping("/editIdentify")
    @ApiOperation("修改农技病害查询")
    public AjaxResult edit(@RequestBody AgriculturalIdentify agriculturalIdentify)
    {
        if(StringUtils.isEmpty(agriculturalIdentify.getId())) {
            return AjaxResult.error("id不能为空");
        }
        AgriculturalIdentify byId = agriculturalIdentifyService.getById(agriculturalIdentify.getId());
        if(byId == null) {
            return AjaxResult.error("信息未找到");
        }
        if(byId.getStatus() == 1) {
            return AjaxResult.error("该信息已发布，情先下架以后再修改");
        }
        agriculturalIdentify.setUpdateTime(DateUtils.getNowDate());
        agriculturalIdentify.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return toAjax(agriculturalIdentifyService.updateById(agriculturalIdentify));
    }

    @GetMapping("/deleteIdentifyByIds")
    @ApiOperation("删除农技病害查询")
    public AjaxResult remove(String ids)
    {
        if(StringUtils.isEmpty(ids)) {
            return AjaxResult.error("id不能为空");
        }
        List<String> list = Arrays.asList(ids.split(","));
        List<AgriculturalIdentify> agriculturalIdentifyList = agriculturalIdentifyService.listByIds(list);
        if(agriculturalIdentifyList.size() != list.size()) {
            String temp = "";
            for (AgriculturalIdentify s : agriculturalIdentifyList) {
                if(!list.contains(s.getId())) {
                    temp = temp + s.getId();
                }
            }
            return AjaxResult.error("id信息未找到： "+temp);
        }
        for (AgriculturalIdentify s : agriculturalIdentifyList) {
            if(s.getStatus() != 0) {
                return AjaxResult.error("信息未下架，请先下架以后再进行删除： "+s.getId());
            }
        }

        return toAjax(agriculturalIdentifyService.removeByIds(list));
    }

    @GetMapping("/onlineIdentify")
    @ApiOperation("发布农技病害查询")
    public AjaxResult onlineIdentify(String id)
    {
        if(StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        AgriculturalIdentify byId = agriculturalIdentifyService.getById(id);
        if(byId == null) {
            return AjaxResult.error("信息未找到");
        }
        if(byId.getStatus() != 0) {
            return AjaxResult.error("该信息已上架");
        }
        List<AgriculturalIdentify> list = agriculturalIdentifyService.
                list(new LambdaQueryWrapper<AgriculturalIdentify>().
                        eq(AgriculturalIdentify::getDepartId,byId.getDepartId()).
                        eq(AgriculturalIdentify::getStatus, 1));
        if(!list.isEmpty()) {
            List<String> collect = list.stream().map(s -> s.getId()).collect(Collectors.toList());
            agriculturalIdentifyService.update(new LambdaUpdateWrapper<AgriculturalIdentify>().
                    set(AgriculturalIdentify::getStatus,0).
                    in(AgriculturalIdentify::getId,collect));

        }
        AgriculturalIdentify agriculturalIdentify = new AgriculturalIdentify();
        agriculturalIdentify.setId(id);
        agriculturalIdentify.setStatus(1);
        agriculturalIdentify.setUpdateTime(DateUtils.getNowDate());
        agriculturalIdentify.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return toAjax(agriculturalIdentifyService.updateById(agriculturalIdentify));
    }

    @GetMapping("/offlineIdentify")
    @ApiOperation("下架农技病害查询")
    public AjaxResult offlineIdentify(String id)
    {
        if(StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        AgriculturalIdentify byId = agriculturalIdentifyService.getById(id);
        if(byId == null) {
            return AjaxResult.error("信息未找到");
        }
        if(byId.getStatus() != 1) {
            return AjaxResult.error("该信息已下架");
        }
        AgriculturalIdentify agriculturalIdentify = new AgriculturalIdentify();
        agriculturalIdentify.setId(id);
        agriculturalIdentify.setStatus(0);
        agriculturalIdentify.setUpdateTime(DateUtils.getNowDate());
        agriculturalIdentify.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return toAjax(agriculturalIdentifyService.updateById(agriculturalIdentify));
    }

    @GetMapping("/getIdentifySelectData")
    @ApiOperation("获取下拉选择")
    public AjaxResult getSelectData(String departId)
    {
        LambdaQueryWrapper<AgriculturalIdentify> queryWrapper = new LambdaQueryWrapper<>();
        //传了token优先使用，如果未传token传了departId 用depaartid
        String depart = departId;
        try {
            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                depart = String.valueOf(SecurityUtils.getUserDepartId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        if(StringUtils.isNotEmpty(depart)) {
            queryWrapper.eq(AgriculturalIdentify::getDepartId,depart);
        }
        queryWrapper.groupBy(AgriculturalIdentify::getIdentify);
        List<String> collect = agriculturalIdentifyService.list(queryWrapper).stream().map(s -> s.getIdentify()).collect(Collectors.toList());
        return AjaxResult.success(collect);
    }

    @GetMapping("/getScreenIdentifyHH")
    @ApiOperation("大屏获取红花基地")
    public AjaxResult getScreenIdentifyHH(String name)
    {
        LambdaQueryWrapper<AgriculturalIdentify> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgriculturalIdentify::getDepartId,101)
                .eq(AgriculturalIdentify::getStatus,1);
        if(StringUtils.isNotEmpty(name)) {
            queryWrapper.eq(AgriculturalIdentify::getIdentify,name);
        }
        queryWrapper.orderByDesc(AgriculturalIdentify::getUpdateTime);
        return AjaxResult.success(agriculturalIdentifyService.list(queryWrapper));
    }

    @GetMapping("/getScreenIdentifyCYG")
    @ApiOperation("大屏获取茶叶谷基地")
    public AjaxResult getScreenIdentifyCYG(String name)
    {
        LambdaQueryWrapper<AgriculturalIdentify> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgriculturalIdentify::getDepartId,105)
                .eq(AgriculturalIdentify::getStatus,1);
        if(StringUtils.isNotEmpty(name)) {
            queryWrapper.eq(AgriculturalIdentify::getIdentify,name);
        }
        queryWrapper.orderByDesc(AgriculturalIdentify::getUpdateTime);
        return AjaxResult.success(agriculturalIdentifyService.list(queryWrapper));
    }


}
